#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

class Solution {
    public:
	vector<int> loudAndRich(vector<vector<int>> &richer, vector<int> &quiet)
	{
		int n = quiet.size(), m = richer.size();
		vector<int> head(n, -1), next(m), to(m), indgree(n), ans(n);
		int cnt = 0;
		for (auto &v : richer) {
			next[cnt] = head[v[0]];
			to[cnt] = v[1];
			head[v[0]] = cnt++;
			indgree[v[1]]++;
		}

		queue<int> q;
		for (int i = 0; i < indgree.size(); i++) {
			if (indgree[i] == 0) {
				q.push(i);
			}
			ans[i] = i;
		}

		while (!q.empty()) {
			int u = q.front();
			q.pop();
			for (int e = head[u]; e != -1; e = next[e]) {
				int v = to[e];
				if (quiet[ans[u]] < quiet[ans[v]]) {
					ans[v] = ans[u];
				}
				if (--indgree[v] == 0) {
					q.push(v);
				}
			}
		}
		return ans;
	}
};

int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	vector<vector<int>> richer = { { 1, 0 }, { 2, 1 }, { 3, 1 }, { 3, 7 }, { 4, 3 }, { 5, 3 }, { 6, 3 } };
	vector<int> quiet = { 3, 2, 5, 4, 6, 1, 7, 0 };
	vector<int> ans = Solution().loudAndRich(richer, quiet);
	for (auto v : ans) {
		cout << v << " ";
	}
	cout << endl;
	return 0;
}